/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package olympicgamesgui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import olympicgameslogic.*;

/**
 *
 * @author Nuno
 */
public class DialogRankingDiscipline extends JDialog {

   private JComboBox editionCombo;
   private JComboBox modalityCombo, modalityCombo2, disciplineCombo, disciplineCombo2;
   private JPanel select, psul;
   private ImageIcon img;
   private String tit;
   private JTable tabela1, tabela2;
   ClassLoader cldr = this.getClass().getClassLoader();
   String[] columnNames = {"#", "ICO", "Name", "Gold", "Silver", "Bronze", "Total"};
   private DefaultTableModel TableModel1, TabelModel2;
   private String titulo;
   private JScrollPane painel1, painel2;
   private Object[][] data1, data2;
   private JTabbedPane pCentroTab;

   public DialogRankingDiscipline(Frame owner, String titulo) {


      super(owner, "Ranking " + titulo, true);
      this.titulo = titulo;
      super.setIconImage(Toolkit.getDefaultToolkit().getImage(cldr.getResource("images/extra/icon.png")));
      img = new ImageIcon(cldr.getResource("images/extra/favorite.png"));

//aqui vamos definir qual o ranking que será aberto dependendo do que e passado como atributo           

      tit = "Discipline";

      setLayout(new BorderLayout());

      setBackground(Color.white);
      //    Criação da barra de topo
      JPanel pannorte = new JPanel(new GridLayout(1, 2));
      pannorte.setSize(200, 60);
      pannorte.setBackground(Color.white);

      JLabel background = new JLabel(tit, JLabel.CENTER);
      background.setFont(new Font("Arial", Font.BOLD, 14));
      background.setIcon(img);
      pannorte.add(background);
      add(pannorte, BorderLayout.NORTH);

      select = new JPanel(new FlowLayout(FlowLayout.LEFT));
      JLabel edic = new JLabel("Edition");
      edic.setOpaque(false);
      select.setBackground(Color.white);
      select.setOpaque(false);
      editionCombo = new JComboBox(OlympicGamesGui.ap.getEditions().toArray());
      editionCombo.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    JComboBox box = (JComboBox) (e.getSource());
	    if (box.getSelectedIndex() > -1 && modalityCombo.getSelectedIndex() > -1 && disciplineCombo.getSelectedIndex() > -1) {
	       refreshTable1(false);
	    }
	 }
      });



      select.add(edic);
      select.add(editionCombo);
      modalityCombo = new JComboBox(OlympicGamesGui.ap.getModalities().toArray());
      modalityCombo.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    JComboBox box = (JComboBox) (e.getSource());
	    if (box.getSelectedIndex() > -1 && disciplineCombo.getSelectedIndex() > -1) {
	       Modality mod = OlympicGamesGui.ap.getModalities().getElement(modalityCombo.getSelectedIndex());
	       LinkedList<Discipline> disciplines = mod.getDisciplines();
	       disciplineCombo.setModel(new DefaultComboBoxModel(disciplines.toArray()));

	    }



	 }
      });
      select.add(modalityCombo);
      disciplineCombo = new JComboBox(OlympicGamesGui.ap.getModalities().getElement(modalityCombo.getSelectedIndex()).getDisciplines().toArray());
      disciplineCombo.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    JComboBox box = (JComboBox) (e.getSource());
	    if (box.getSelectedIndex() > -1) {
	       refreshTable1(false);
	    }

	 }
      });
      select.add(disciplineCombo);


      JButton bthtml1 = new JButton("HTML Export");
      bthtml1.setFont(new Font("Arial", Font.PLAIN, 8));
      bthtml1.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    refreshTable1(true);
	    JOptionPane.showMessageDialog(DialogRankingDiscipline.this, "HTML Exporter");
	 }
      });
      select.add(bthtml1);

//tabela1 (1º tab      
      TableModel1 = new DefaultTableModel(data1, columnNames);
      TableModel1.setColumnIdentifiers(columnNames);
      tabela1 = new JTable() {

	 public boolean isCellEditable(int rowIndex, int colIndex) {
	    return false; //Disallow the editing of any cell
	 }

	 public Class getColumnClass(int column) {
	    if (column == 1) {
	       return ImageIcon.class;
	    }
	    return Object.class;
	    //return getValueAt(0, column).getClass();
	 }
      };
      setUpTableData1(false);

      JPanel p1 = new JPanel(new BorderLayout());
      painel1 = new JScrollPane(tabela1);
      p1.add(select, BorderLayout.NORTH);
      p1.add(painel1, BorderLayout.CENTER);

//tabela2 (2º tab)
      TabelModel2 = new DefaultTableModel(data2, columnNames);
      TabelModel2.setColumnIdentifiers(columnNames);
      tabela2 = new JTable() {

	 public boolean isCellEditable(int rowIndex, int colIndex) {
	    return false; //Disallow the editing of any cell
	 }

	 public Class getColumnClass(int column) {
	    if (column == 1) {
	       return ImageIcon.class;
	    }
	    return Object.class;
	    //return getValueAt(0, column).getClass();
	 }
      };

      JPanel p2 = new JPanel(new BorderLayout());
      painel2 = new JScrollPane(tabela2);
      JButton bthtml2 = new JButton("HTML Export");
      bthtml2.setFont(new Font("Arial", Font.PLAIN, 8));
      bthtml2.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    JOptionPane.showMessageDialog(DialogRankingDiscipline.this, "HTML Exporter 2");
	 }
      });
      JPanel select2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
      modalityCombo2 = new JComboBox(OlympicGamesGui.ap.getModalities().toArray());
      modalityCombo2.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    JComboBox box = (JComboBox) (e.getSource());
	    if (box.getSelectedIndex() > -1 && disciplineCombo2.getSelectedIndex() > -1) {
	       Modality mod = OlympicGamesGui.ap.getModalities().getElement(modalityCombo2.getSelectedIndex());
	       LinkedList<Discipline> disciplines = mod.getDisciplines();
	       disciplineCombo2.setModel(new DefaultComboBoxModel(disciplines.toArray()));
	    }
	 }
      });
      select2.add(modalityCombo2);


      disciplineCombo2 = new JComboBox(OlympicGamesGui.ap.getModalities().getElement(modalityCombo2.getSelectedIndex()).getDisciplines().toArray());
      disciplineCombo2.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    JComboBox box = (JComboBox) (e.getSource());
	    if (box.getSelectedIndex() > -1) {
	       refreshTable2(false);
	    }

	 }
      });
      select2.add(disciplineCombo2);
      setUpTableData2(false);

      select2.add(bthtml2);

      p2.add(select2, BorderLayout.NORTH);
      p2.add(painel2, BorderLayout.CENTER);

//definiçaõ de tabs carregando os 2 paineis                          
      pCentroTab = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
      pCentroTab.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

      pCentroTab.addTab("Edition", null, p1, "Edition");
      pCentroTab.addTab("History", null, p2, "History");

      add(pCentroTab, BorderLayout.CENTER);



      setSize(460, 500);
      //setResizable(false);
      setLocationRelativeTo(null);

      setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      


   }

   public void showJan() {

      setVisible(true);







   }

   private void setUpTableData1(boolean export) {

      tabela1.removeAll();
      Modality mod = OlympicGamesGui.ap.getModalities().getElement(modalityCombo.getSelectedIndex());

      Edition e = OlympicGamesGui.ap.getEditions().getElement(editionCombo.getSelectedIndex());
      Discipline disc = mod.getDisciplines().getElement(disciplineCombo.getSelectedIndex());
      LinkedList<Delegation> delegationEditionList = new LinkedList<>();
      LinkedList<Delegation> tempList = new LinkedList<>();
      delegationEditionList = e.getEditionDelegationList();


      for (Delegation delegation : delegationEditionList) {
	 if (delegation.getDisciplineMedals(disc).size() > 0) {
	    tempList.add(delegation);
	 }

      }





      Collections.sort(tempList, new Delegation.MedalsWeightedComparator());

      data1 = new Object[tempList.getSize()][7];
      int i = 0;
      for (Delegation d : tempList) {
	 int gold = 0, silver = 0, bronze = 0, total = 0;

	 for (Medal medal : d.getDelegationMedals()) {
	    if (medal.getDiscipline().equals(disc)) {
	       total++;
	       if (medal.getMedalType().equals(Medal.MEDALTYPE.GOLD)) {
		  gold++;
	       } else if (medal.getMedalType().equals(Medal.MEDALTYPE.SILVER)) {
		  silver++;
	       } else if (medal.getMedalType().equals(Medal.MEDALTYPE.BRONZE)) {
		  bronze++;
	       }
	    }
	 }

	 data1[i][0] = "" + (i + 1);
	 String img = "images/flags/" + (d.getCountry().getCode()) + ".png";
	 if (cldr.getResource(img) == null)
	    img = "images/extra/btcancel.png";
	 data1[i][1] = new ImageIcon(cldr.getResource(img));
	 data1[i][2] = d.getCountry().getName();
	 data1[i][3] = gold;
	 data1[i][4] = silver;
	 data1[i][5] = bronze;
	 data1[i][6] = total;
	 TableModel1.addRow(data1[i]);
	 i++;
	 if (i == 10) {
	    break;
	 }
      }
      tabela1.setModel(TableModel1);
      DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
      rightRenderer.setHorizontalAlignment(JLabel.CENTER);
      tabela1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      TableColumn col = tabela1.getColumnModel().getColumn(0);
      col.setPreferredWidth(30);
      col.setCellRenderer(rightRenderer);
      col = tabela1.getColumnModel().getColumn(1);
      col.setPreferredWidth(30);
      col = tabela1.getColumnModel().getColumn(2);
      col.setPreferredWidth(180);
      col = tabela1.getColumnModel().getColumn(3);
      col.setPreferredWidth(50);
      col.setCellRenderer(rightRenderer);
      col = tabela1.getColumnModel().getColumn(4);
      col.setPreferredWidth(50);
      col.setCellRenderer(rightRenderer);
      col = tabela1.getColumnModel().getColumn(5);
      col.setPreferredWidth(50);
      col.setCellRenderer(rightRenderer);
      col = tabela1.getColumnModel().getColumn(6);
      col.setPreferredWidth(50);
      col.setCellRenderer(rightRenderer);
      JTableHeader header = tabela1.getTableHeader();
      header.setBackground(Color.DARK_GRAY);
      header.setFont(new Font("Arial", Font.BOLD, 12));
      header.setForeground(Color.WHITE);
      tabela1.setPreferredScrollableViewportSize(new Dimension(300, 70));
      tabela1.setFillsViewportHeight(true);
      tabela1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      tabela1.getTableHeader().setReorderingAllowed(false);
      tabela1.getTableHeader().setResizingAllowed(false);
   }

   //Historico
   private void setUpTableData2(boolean export) {

      tabela2.removeAll();
      Modality m = OlympicGamesGui.ap.getModalities().getElement(modalityCombo2.getSelectedIndex());
      Discipline disc = m.getDisciplines().getElement(disciplineCombo2.getSelectedIndex());
      LinkedList<Nation> nts = OlympicGamesGui.ap.getNations();
      LinkedList<Nation> tempList = new LinkedList<>();
      for (Nation nation : nts) {
	 if (nation.getDisciplineMedals(disc).size() > 0) {
	    tempList.add(nation);

	 }

      }

      Collections.sort(tempList, new Nation.MedalsAbsoluteComparator());

      data2 = new Object[tempList.getSize()][7];
      int i = 0;
      for (Nation n : tempList) {
	 int gold = 0, silver = 0, bronze = 0, total = 0;
	 for (Medal medal : n.getNationMedals()) {
	    if (medal.getModality().equals(m)) {
	       total++;
	       if (medal.getMedalType().equals(Medal.MEDALTYPE.GOLD)) {
		  gold++;
	       } else if (medal.getMedalType().equals(Medal.MEDALTYPE.SILVER)) {
		  silver++;
	       } else if (medal.getMedalType().equals(Medal.MEDALTYPE.BRONZE)) {
		  bronze++;
	       }
	    }
	 }

	 data2[i][0] = "" + (i + 1);
	 String img = "images/flags/" + (n.getCode()) + ".png";
	 if (cldr.getResource(img) == null)
	    img = "images/extra/btcancel.png";
	 data2[i][1] = new ImageIcon(cldr.getResource(img));
	 data2[i][2] = n.getName();
	 data2[i][3] = gold;
	 data2[i][4] = silver;
	 data2[i][5] = bronze;
	 data2[i][6] = total;
	 TabelModel2.addRow(data2[i]);
	 i++;
	 if (i == 10) {
	    break;
	 }
      }
      tabela2.setModel(TabelModel2);
      DefaultTableCellRenderer rightRenderer2 = new DefaultTableCellRenderer();
      rightRenderer2.setHorizontalAlignment(JLabel.CENTER);
      tabela2.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      TableColumn col2 = tabela2.getColumnModel().getColumn(0);
      col2.setPreferredWidth(30);
      col2.setCellRenderer(rightRenderer2);
      col2 = tabela2.getColumnModel().getColumn(1);
      col2.setPreferredWidth(30);
      col2 = tabela2.getColumnModel().getColumn(2);
      col2.setPreferredWidth(180);
      col2 = tabela2.getColumnModel().getColumn(3);
      col2.setPreferredWidth(50);
      col2.setCellRenderer(rightRenderer2);
      col2 = tabela2.getColumnModel().getColumn(4);
      col2.setPreferredWidth(50);
      col2.setCellRenderer(rightRenderer2);
      col2 = tabela2.getColumnModel().getColumn(5);
      col2.setPreferredWidth(50);
      col2.setCellRenderer(rightRenderer2);
      col2 = tabela2.getColumnModel().getColumn(6);
      col2.setPreferredWidth(50);
      col2.setCellRenderer(rightRenderer2);
      JTableHeader header2 = tabela2.getTableHeader();
      header2.setBackground(Color.DARK_GRAY);
      header2.setFont(new Font("Arial", Font.BOLD, 12));
      header2.setForeground(Color.WHITE);
      tabela2.setPreferredScrollableViewportSize(new Dimension(300, 70));
      tabela2.setFillsViewportHeight(true);
      tabela2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      tabela2.getTableHeader().setReorderingAllowed(false);
      tabela2.getTableHeader().setResizingAllowed(false);
   }

   public void refreshTable1(boolean export) {
      TableModel1.setRowCount(0);
      tabela1.removeAll();
      setUpTableData1(export);
   }

   public void refreshTable2(boolean export) {
      TabelModel2.setRowCount(0);
      tabela2.removeAll();
      setUpTableData2(export);
   }
}
